/*
 * EmployeeEditServlet.java
 * Created on 25. juni 2008, 12:11
 */
package webshop.del;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webshop.data.del.Department;
import webshop.data.del.Employee;



/**
 * EmployeeEditServlet
 * @author even
 */
public class EmployeeEditServlet  extends AbstractDepartmentServlet {

    
    /**
     * Handle requests. Sends a html page containing the department list.
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();
        
        Employee emp = null;
        Department dep = null;
        String empnoString = request.getParameter("empno");
        String depnoString = request.getParameter("depno");
        
        if (empnoString != null) {
            long empno = Long.valueOf(empnoString);
            emp = em.find(Employee.class, empno);
        }
        
        if (emp != null) dep = emp.getDepartment();
        else if (depnoString != null) {
            long depno = Long.valueOf(depnoString);
            dep = em.find(Department.class, depno);
        }

        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        out.println("<html><head><title>"+this.getClass().getName()+"</title></head><body>");
        
        if (emp != null) {
            out.println("<h1>Edit Employee</h1>");
            out.println("<form action=\"empsave\">");
            out.println("<input type=\"hidden\" name=\"empno\" value=\""+emp.getEmpNo()+"\"/>");
            out.println("<table>");
            printDatum(out, "Emp.no", emp.getEmpNo());
            printField(out, "Name", "name", emp.getName());
            printField(out, "Salary", "salary", emp.getSalary());
            printDepartmentSelector(out, em, dep);
            out.println("</table>");
            out.println("<input type=\"submit\"/>");
            out.println("</form>");
        }
        else {
            out.println("<h1>New Employee</h1>");
            out.println("<form action=\"empsave\">");
            out.println("<table>");
            printField(out, "Name", "name", "");
            printField(out, "Salary", "salary", 0L);
            printDepartmentSelector(out, em, dep);
            out.println("</table>");
            out.println("<input type=\"submit\"/>");
            out.println("</form>");
            
        }
        
        out.println("<hr/>");
        out.println("<p><a href=\"index.html\">Home</a></p>");
        out.println("</body>");
        out.println("</html>");
        
        em.close();
    }
    
    
    private void printDepartmentSelector(PrintWriter out, EntityManager em, Department selected) {
        List<Department> deps = em.createNamedQuery("Department.selectAll").getResultList();
        out.println("<tr><td>Department:</td><td>");
        out.println("<select name=\"depno\">");
        for (Department dep : deps) {
            out.print("<option ");
            if (dep.equals(selected)) out.print("selected ");
            out.print("value=\""+dep.getDepNo()+"\">"+dep.getName()+"</option>");
            out.println();
        }
        out.println("</selecct></td></tr>");
    }
}
